using Nop.Core.Domain.Media;
using System.Collections.Generic;
using System;
using Nop.Core.Data;
using System.Linq;
using Nop.Core.Infrastructure;
using Nop.Core;

namespace Nop.Services.Media
{
    public class PictureDisplayService : IPictureDisplayService
    {
        private readonly IRepository<Slideshow> _slideshowRepository;
        private readonly IRepository<PictureLinkCollection> _pictureLinkCollectionRespository;
        private readonly IRepository<PictureLink> _pictureLinkRespository;

        private ContentResolver _contentResolver;

        public PictureDisplayService(
            IRepository<Slideshow> slideshowRepository,
            IRepository<PictureLinkCollection> pictureLinkCollectionRespository,
            IRepository<PictureLink> pictureLinkRespository
            )
        {
            this._slideshowRepository = slideshowRepository;
            this._pictureLinkRespository = pictureLinkRespository;
            this._pictureLinkCollectionRespository = pictureLinkCollectionRespository;

            this._contentResolver = new ContentResolver(EngineContext.Current.Resolve<ITypeFinder>());
        }
        public void InsertPictureLink(PictureLink pictureLink)
        {
            if (pictureLink == null)
                throw new ArgumentNullException("pictureLink");

            //update
            _pictureLinkRespository.Insert(pictureLink);
        }

        public void UpdatePictureLink(PictureLink pictureLink)
        {
            if (pictureLink == null)
                throw new ArgumentNullException("pictureLink");

            //update
            _pictureLinkRespository.Update(pictureLink);
        }

        public void DeletePictureLink(PictureLink pictureLink)
        {
            if (pictureLink == null)
                throw new ArgumentNullException("pictureLink");

            //update
            _pictureLinkRespository.Delete(pictureLink);
        }

        public PictureLink GetPictureLinkById(int PictureLinkId)
        {
            var pictureLink = _pictureLinkRespository.GetById(PictureLinkId);
            return pictureLink;
        }

        public void InsertSlideshow(Slideshow slideshow)
        {
            if (slideshow == null)
                throw new ArgumentNullException("slideshow");

            _slideshowRepository.Insert(slideshow);
        }

        public void UpdateSlideshow(Slideshow slideshow)
        {
            if (slideshow == null)
                throw new ArgumentNullException("slideshow");

            _slideshowRepository.Update(slideshow);
        }

        public void DeleteSlideshow(Slideshow slideshow)
        {
            if (slideshow == null)
                throw new ArgumentNullException("slideshow");

            _slideshowRepository.Delete(slideshow);
        }

        public void InsertPictureLinkCollection(PictureLinkCollection pictureLinkCollection)
        {
            if (pictureLinkCollection == null)
                throw new ArgumentNullException("pictureLinkCollection");

            //update
            _pictureLinkCollectionRespository.Insert(pictureLinkCollection);
        }

        public void UpdatePictureLinkCollection(PictureLinkCollection pictureLinkCollection)
        {
            if (pictureLinkCollection == null)
                throw new ArgumentNullException("pictureLinkCollection");

            //update
            _pictureLinkCollectionRespository.Update(pictureLinkCollection);
        }

        public void DeletePictureLinkCollection(PictureLinkCollection pictureLinkCollection)
        {
            if (pictureLinkCollection == null)
                throw new ArgumentNullException("pictureLinkCollection");

            //update
            _pictureLinkCollectionRespository.Delete(pictureLinkCollection);
        }

        public PictureLinkCollection GetPictureLinkCollectionById(int pictureLinkCollectionId)
        {
            var pictureLinkCollection = _pictureLinkCollectionRespository.GetById(pictureLinkCollectionId);
            return pictureLinkCollection;
        }

        public IList<PictureLink> GetPictureLinks(int pictureLinkCollectionId)
        {
            var query = from pl in _pictureLinkRespository.Table
                        where (pl.CollectionId == pictureLinkCollectionId)
                        orderby pl.SystemName
                        select pl;
            var picturelinksList = query.ToList();
            return picturelinksList;
        }

        public IPagedList<PictureLink> GetPictureLinks(int pictureLinkCollectionId, int pageIndex, int pageSize)
        {
            var picturelinksList = GetPictureLinks(pictureLinkCollectionId);
            return new PagedList<PictureLink>(picturelinksList, pageIndex, pageSize);
        }

        public IList<PictureLink> GetVisiblePictureLinks(int pictureLinkCollectionId)
        {
            var query = from pl in _pictureLinkRespository.Table
                        where (pl.CollectionId == pictureLinkCollectionId) &&
                        (pl.Visible)
                        orderby pl.SystemName
                        select pl;
            var picturelinksList = query.ToList();
            return picturelinksList;
        }

        public IList<PictureLink> GetAllPictureLinks()
        {
            var query = from pl in _pictureLinkRespository.Table
                        orderby pl.SystemName
                        select pl;
            var picturelinksList = query.ToList();
            return picturelinksList;
        }

        public IPagedList<PictureLink> GetAllPictureLinks(int pageIndex, int pageSize)
        {
            var query = from pl in _pictureLinkRespository.Table
                        orderby pl.SystemName
                        select pl;
            return new PagedList<PictureLink>(query, pageIndex, pageSize);
        }

        public IList<PictureLinkCollection> GetAllPictureLinkCollections()
        {
            var query = from s in _pictureLinkCollectionRespository.Table
                        orderby s.SystemName
                        select s;
            return query.ToList();
        }

        public IPagedList<PictureLinkCollection> GetAllPictureLinkCollections(int pageIndex, int pageSize)
        {
            var picturelinkCollectionsList = GetAllPictureLinkCollections();
            return new PagedList<PictureLinkCollection>(picturelinkCollectionsList, pageIndex, pageSize);
        }

        public IList<PictureLinkCollection> GetAllPictureLinkCollectionsByType(PictureDisplayType type)
        {
            var query = from s in _pictureLinkCollectionRespository.Table
                        orderby s.SystemName
                        where ( (s.PictureDisplayType & PictureDisplayType.Slideshow) == PictureDisplayType.Slideshow )
                        select s;
            return query.ToList();
        }

        public Slideshow GetSlideshowById(int slidershowId)
        {
            var slideshow = _slideshowRepository.GetById(slidershowId);
            return slideshow;
        }

        public Slideshow GetSlideshowBySystemName(string systemName)
        {
			var query = from s in _slideshowRepository.Table
						where s.PictureLinkCollection != null && s.PictureLinkCollection.SystemName.Equals(systemName, StringComparison.OrdinalIgnoreCase)
						select s;
			return query.FirstOrDefault();
        }

        public IList<Slideshow> GetSlideshowsByType(PictureDisplayType type)
        {
            var query = from s in _slideshowRepository.Table
                        where ((s.PictureLinkCollection.PictureDisplayType & type) == type)
                        select s;
            return query.ToList();
        }

        public IList<Slideshow> GetSlideshowsByTypeAndContentCategory(PictureDisplayType? type,
                string contentCategory, int contentId, bool? enabled)
        {
            IQueryable<Slideshow> query;
            if (contentCategory == null)
            {
                query = from s in _slideshowRepository.Table
                        where ( type == null || (s.PictureLinkCollection.PictureDisplayType & type) == type) &&
                              (s.PictureLinkCollection.RelatedContentCategory == null) &&
                              (s.PictureLinkCollection.RelatedContentId == contentId) &&
                              (!enabled.HasValue || s.Enabled == enabled.Value )
                        select s;
            }
            else
            {
                query = from s in _slideshowRepository.Table
                        where ( type == null || (s.PictureLinkCollection.PictureDisplayType & type) == type) &&
                              (s.PictureLinkCollection.RelatedContentCategory.ToLower() == contentCategory.ToLower()) &&
                              (s.PictureLinkCollection.RelatedContentId == contentId) &&
                              (!enabled.HasValue || s.Enabled == enabled.Value)
                        select s;
            }

            return query.ToList();
        }

        public IList<Slideshow> GetAllSlideshows()
        {
            var query = from ss in _slideshowRepository.Table
                             orderby ss.ShowTitle
                             select ss;
            var slideshows = query.ToList();
            return slideshows;
        }

        public IList<System.Web.Mvc.SelectListItem> GetRelatedContantSelectListByCategoryName(string categoryName, int selectedContentId = 0)
        {
            return _contentResolver.GetRelatedContantSelectList(categoryName, selectedContentId);
        }

        public IList<string> GetAllRelatedContentCategoryNames()
        {
            return _contentResolver.GetAllRelatedContentCategoryNames();
        }
    }
}
